#include "StdAfx.h"
#include "TurnPlay.h"
using namespace SHGame;
TurnPlay::TurnPlay(void)
{
}
Player^ TurnPlay::GetPlayerAt(int i)
{
	return TurnPlay::parr[i];
}
void TurnPlay::SetPlayerAt(int i, Player^ p)
{
	TurnPlay::parr[i] = p;
}

/*
Progresses to the next player.
If there is no next player, fires a TurnEndEvent.
If the turns have not started, returns false.
Only returns true if there is a next player.
Fires a TurnPlayEvent if returned true.
*/
bool TurnPlay::MoveNext()
{
	if (turn == -1)
		// we are not in a turn-system, need to start
		throw gcnew InvalidOperationException("Cannot move next without starting.");
	Diagnostics::Debug::WriteLine("Next turn: turn=" + turn + "  start="+start);
	turn++;
	if (turn != start && turn % parr->Length == start)
	{
		// we are at the end, the last player played
		turn = -1;
		// cache old player
		Player^ old = Current;
		// no next player, we're at the end
		Current = nullptr;
		pcs->Fire(TurnEndEvent, old, nullptr);
		// there is no next player
		return false;
	}
	// cache old player
	Player^ old = Current;
	Current = parr[turn % 5];
	pcs->Fire(TurnNextEvent, old, Current);
	return true;
}

/*
Returns the human player. This is, by default, the first player in the
array of players.
*/
Player^ TurnPlay::GetHuman()
{
	return TurnPlay::parr[0];
}
/*
Starts a new set of turns with the given player as the starting player.
This will only work if we are not in a turn system at the moment.
Throws an exception if there is no current player
*/
void TurnPlay::SetStart(Player^ lead)
{
	Diagnostics::Debug::WriteLine("Getting a new start...");
	if (turn != -1) {
		throw gcnew InvalidOperationException("Cannot start when turns are going on...");
	}
	int index = GetIndexOf(lead);
	if (index < 0)
		throw gcnew ArgumentException("Given player does not have a turn: not in arr");
	turn = index;
	start = index;
}
void TurnPlay::Start() {
	if (turn != start) {
		throw gcnew InvalidOperationException("Cannot start unless turn=start: " + turn + "!="+start);
	}
	Diagnostics::Debug::WriteLine("************************************************");
	Diagnostics::Debug::WriteLine("                   NEW ROUND");
	Diagnostics::Debug::WriteLine("************************************************");
	Current = parr[turn];
	pcs->Fire(TurnStartEvent, nullptr, Current);
}

bool TurnPlay::ReadyForStart() {
	return turn == start;
}

int TurnPlay::GetIndexOf(Player^ lead)
{
	for (int i = 0; i < parr->Length; i++) {
		if (parr[i] == lead)
			return i;
	}
	return -1;
}

bool TurnPlay::IsHumanTurn()
{
	return GetHuman() == Current;
}

void TurnPlay::Stop() 
{
	turn = -1;
}

array<Player^>^ TurnPlay::GetRest()
{
	if (turn != start && turn % parr->Length == start)
	{
		// we are on the end... return empty
		array<Player^> ^r = { };
		return r;
	}
	if (turn % parr->Length == start) {
		// we are on the lead
		
	}
	int tturn = turn + 1;
	int l = 0;
	while ( tturn % parr->Length != start )
	{
		l++;
		tturn++;
	}
	int i = 0;
	array<Player^> ^ arr = gcnew array<Player^>(l);
	tturn = turn + 1;
	while ( tturn % parr->Length != start ) 
	{
		arr[i] = parr[tturn % parr->Length];
		tturn++;
		i++;
	}
	return arr;
}